package com.itheima.admin.controller;

import com.itheima.admin.dto.AdSensitivePageRequestDto;
import com.itheima.admin.pojo.AdSensitive;
import com.itheima.admin.service.AdSensitiveService;
import com.itheima.common.enums.HttpCodeEnum;
import com.itheima.common.vo.PageResultVo;
import com.itheima.common.vo.ResultVo;
import com.itheima.core.controller.AbstractCoreController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @version 1.0
 * @description 说明
 * @package com.itheima.admin.controller
 */
@RestController
@RequestMapping("/sensitive")
@Api(tags = "敏感词管理")
public class AdSensitiveController extends AbstractCoreController<AdSensitive> {

    @Autowired
    private AdSensitiveService adSensitiveService;

    @Autowired
    public AdSensitiveController(AdSensitiveService adSensitiveService){
        super(adSensitiveService);
    }

    /**
     * 敏感词分页查询
     * @param dto
     * @return
     */
    @PostMapping("/list")
    @ApiOperation("敏感词分页搜索")
    public PageResultVo<AdSensitive> findPage(@RequestBody AdSensitivePageRequestDto dto){
        PageResultVo<AdSensitive> result = adSensitiveService.findPage(dto);
        return result;
    }

    /**
     * 添加敏感词
     * @param adSensitive
     * @return
     */
    @PostMapping("/save")
    @ApiOperation("添加敏感词")
    @Override
    public ResultVo insert(@RequestBody AdSensitive adSensitive){
        adSensitiveService.add(adSensitive);
        return ResultVo.ok();
    }

    /**
     * 更新数据
     * @param adSensitive
     * @return
     */
    @PostMapping("/update")
    @ApiOperation("更新敏感词")
    @Override
    public ResultVo updateByPrimaryKey(@RequestBody AdSensitive adSensitive){
        if (adSensitive.getId()==null) {
            return ResultVo.error(HttpCodeEnum.PARAM_REQUIRE);
        }
        boolean success = adSensitiveService.updateById(adSensitive);
        if(!success){
            return ResultVo.error();
        }
        return ResultVo.ok();
    }

}
